$.post(
    '/cart/cartListSelectById',
    function (result) {
        if (result.code == 0) {
            var html = '';
            $(result.data).each(function () {
                html += '<div class="list list-item" id="'+this.id+'">'
                html += '   <input id="item-id" type="hidden" value="'+this.id+'">'
                if (this.checked == 1) {
                    html += '	<div class="select"><i class="fa fa-check checked"></i></div>'
                } else {
                    html += '	<div class="select"><i class="fa fa-check"></i></div>'
                }
                html += '	<div class="good-img"><img src="'+this.productImage+'" alt=""></div>'
                html += '	<div class="good-name">'+this.productName+'</div>'
                html += '	<div class="good-price">'+this.price+'元</div>'
                html += '	<div class="good-num">'
                html += '		<div class="num-input">'
                html += '			<button class="minus">-</button>'
                html += '			<input type="text" value="'+this.quantity+'" class="num-value">'
                html += '				<button class="plus">+</button>'
                html += '		</div>'
                html += '	</div>'
                html += '	<div class="good-total-price">'+(this.quantity*this.price)+'元</div>'
                html += '	<div class="operation"><i class="fa fa-times"></i></div>'
                html += '</div>'
            });
            $('#LAY_CartList').after(html);
            doCheckAll();
            doPrice();
        }
    },
    'json'
);


//初始化总价, 总选择数, 总条数;
doPrice();
//全选/选择框的鼠标移上变个颜色
$('.fa-check').mouseover(function(){
    if($(this).attr('class')!='fa fa-check checked') {
        $(this).css('color', '#ff6a00');
    }
})
$('.fa-check').mouseleave(function(){
    $(this).css('color','#fff');
})

//普通勾选
$('#cartListALL').on('click', '.fa-check', function (){
    var cla=$(this).attr('class');
    var checked;
    if(cla!='fa fa-check checked'){
        $(this).attr('class','fa fa-check checked');
        checked = 1;
    }else{
        $(this).attr('class','fa fa-check');
        checked = 0;
    }
    var id = $(this).parents('.list-item').attr('id');
    $.post(
        '/cart/update',
        {'id': id, 'checked': checked},
        function (result) {
            if (result.code == 0) {
                //mylayer.okMsg(result.msg);
                doCheckAll();
                doPrice();
            }
        },
        'json'
    );
})
//删除
$('#cartListALL').on('click', '.fa-times', function (){

    var id = $(this).parents('.list-item').attr('id');
    var that = this;
    layer.confirm(
        '您确定要删除吗？',
        {icon : 3},
        function (index) {
            $.post(
                '/cart/deleteById',
                {'id': id,},
                function (result) {
                    if (result.code == 0) {
                        mylayer.okMsg(result.msg);
                        $(that).parents('.list-item').remove();
                    }
                },
                'json'
            );
        }
    )

})
//全选框勾选
$('#check-all').click(function () {
//$('#cartListALL').on('click', '.check-all', function (){
    var cla=$(this).attr('class');
    var checked;
    if(cla!='fa fa-check checked'){
        $('.fa-check').attr('class','fa fa-check checked');
        checked = 1;
    }else{
        $('.fa-check').attr('class','fa fa-check');
        checked = 0;
    }
    $.post(
        '/cart/updateAll',
        {'checked': checked},
        function (result) {
            if (result.code == 0) {
                //mylayer.okMsg(result.msg);
                //doCheckAll();
                doPrice();
            }
        },
        'json'
    );
})

//检查是否全选
function doCheckAll(){
    var allitem=$('.list-item i[class*="fa-check"]').length;
    var checkeditem=$('.list-item i[class$="checked"]').length;
    if(allitem!=checkeditem){
        $('#check-all').attr('class','fa fa-check');
    }else{
        $('#check-all').attr('class','fa fa-check checked');
    }
}



//加减按钮

//$('button.minus').click(function(){
$('#cartListALL').on('click', 'button.minus', function (){
    var nowvalue=$(this).siblings('input').val();
    nowvalue=parseInt(nowvalue);
    var currentvalue=0;
    nowvalue<=1?currentvalue=1:currentvalue=nowvalue-1;
    $(this).siblings('input').val(currentvalue);


    var id = $(this).parents('.list-item').attr('id');
    var that = this;
    $.post(
        '/cart/update',
        {'id': id, 'quantity': currentvalue},
        function (result) {
            if (result.code == 0) {
                //mylayer.okMsg(result.msg);
                //计算当前的小计
                var danjia=parseFloat($(that).parents('.good-num').siblings('.good-price').html());
                var xiaoji=danjia*currentvalue;
                $(that).parents('.good-num').siblings('.good-total-price').html(xiaoji+'元');

                //更新总价
                doPrice();
            }
        },
        'json'
    );
})

//$('button.plus').click(function(){
$('#cartListALL').on('click', 'button.plus', function (){
    var nowvalue=$(this).siblings('input').val();
    nowvalue=parseInt(nowvalue);
    var currentvalue=nowvalue+1;
    $(this).siblings('input').val(currentvalue);


    var id = $(this).parents('.list-item').attr('id');
    var that = this;
    $.post(
        '/cart/update',
        {'id': id, 'quantity': currentvalue},
        function (result) {
            if (result.code == 0) {
                //mylayer.okMsg(result.msg);
                //计算当前的小计
                var danjia=parseFloat($(that).parents('.good-num').siblings('.good-price').html());
                var xiaoji=danjia*currentvalue;
                $(that).parents('.good-num').siblings('.good-total-price').html(xiaoji+'元');

                //更新总价
                doPrice();
            }
        },
        'json'
    );
})

function doPrice(){
    //统计所有勾选了的值;
    var items=$('.list-item i[class*="fa-check"]');
    var checkeditems=$('.list-item i[class$="checked"]').parents('.select').siblings('.good-total-price')
    var totalprice=0;
    for(var i=0;i<checkeditems.length;i++){
        totalprice+=parseFloat(checkeditems[i].innerHTML);
    }
    //改总价
    $('.sum-price').html(totalprice);
    //改选中数
    $('.select-count').html(checkeditems.length);
    //改总条数
    $('.all-count').html(items.length);

}
